home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_056 / mcad / mp / source / getdat.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  213 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <intuition/intuition.h>
  5. #include "mp.h"
  6. #include "struct.h"
  7.  
  8. #define MAXCOLUMNS 10
  9.  
  10. extern int debug;
  11.  
  12. extern char *getwrd();
  13. extern void trch();
  14. extern int same(), numeric();
  15. struct Plot *GetStructPlot();
  16.  
  17. /***************************************************************************
  18. *     GetDat finds numeric data in a text file and stores it in arrays.
  19. *
  20. *     GetDat reads a text file whose handle is pointed to by 'fp'.  In
  21. *  this file are expected to be lines containing "words" [of arbitrary
  22. *  length, delimited by blanks, tabs, or commas.] In each line, the first
  23. *  word will be pointed to by 'wp[0]'; the user thinks of this
  24. *  word as being in column 1.  'wp[1]' will point to the next word,
  25. *  etc.  GetDat is primarily interested in those words which look like
  26. *  numbers.
  27. *  
  28. *     The user has assigned column numbers to the variables x, y, and e. 
  29. *  She wants GetDat to find all the lines which contain numbers in those
  30. *  columns and save the numbers in the arrays 'x[]', 'y[]', 'e[]'. 
  31. *  Further, successive lines which contain numbers in the columns of
  32. *  interest are to be treated as a list [possibly delimited by lines
  33. *  which don't contain numbers in the columns of interest.]
  34. *  Finally, the number of lists and the numbers of lines
  35. *  in each list are to be saved in the data structure pointed to by the
  36. *  argument 'Pict'. 
  37. *  
  38. *     Special handling: If the column associated with 'x[]' is given as 0,
  39. *  the 'x[]' array is to be filled as if the x-columns of each list
  40. *  contained [0,1,2...].  If the column associated with 'e[]' is 0, the
  41. *  'e[]' array is not to be filled at all.
  42. *  
  43. *     Lines which begin with "*SCALFACT*" contain scale factors by which
  44. *  collected data is to be multiplied.  Scale factor columns map to data
  45. *  columns after the keyword "*SCALFACT*" is discarded. 
  46. *  
  47. *     Lines which begin with "*TITLE*", "*XLABEL*", and "*YLABEL*" are
  48. *  interpreted as text to be stored in the structure pointed to by
  49. *  'Pict'.  The keyword "*...*" and the blanks, tabs, or commas following
  50. *  it are discarded. 
  51. *
  52. *************************************/
  53.  
  54. #define COLUMN_OK(z) (z<0 ? TRUE : numeric(wp[z]))
  55. #define LINE_IS_DATA (COLUMN_OK(xcol) && COLUMN_OK(ycol) && COLUMN_OK(ecol))
  56. #define DATA(z) (fact[(z)]*atoFFP(wp[(z)]))
  57. #define PLOT_OPENED (Plot->NPts && !PlotClosed)
  58.  
  59. FFP fact[MAXCOLUMNS];                /* SCALE FACTORS */
  60. short maxcol;    /* LARGEST OF ARGS: XCOL, YCOL, ECOL */
  61. char title[80], xlabel[80], ylabel[80];
  62.  
  63. /********************************************/
  64. void GetDat(fp, xcol, ycol, ecol, terse, Pict)
  65. FILE *fp;
  66. int xcol, ycol, ecol;   /* column numbers to be inspected */
  67. int terse;              /* false => echo non-numeric lines to stdout */
  68. struct Pict *Pict;      /* contains info for plotting this data */
  69. {
  70.    short i, PlotClosed = FALSE, Continued = FALSE;
  71.    short xcoord = 0;
  72.    char cbuf[100], *cb, *wp[MAXCOLUMNS];
  73.    struct Plot *Plot, *PrevPlot;
  74.    FFP *x, *y, *e;
  75.    
  76. /*** INITIALIZE ***/
  77.  
  78.    /* TRANSLATE from user column numbers to array indices, FIND max */
  79.    xcol--; ycol--; ecol--; maxcol = max(xcol,max(ycol,ecol));
  80.    if (debug) printf("GetDat: x, y, e columns: %d,%d,%d\n",xcol,ycol,ecol);
  81.    for (i=0; i<=maxcol; i++) fact[i] = 1.;   /* init scale factors */
  82.    Pict->NPlt = 0;
  83.    Pict->Plot = GetStructPlot();
  84.    Plot = Pict->Plot;
  85.    x = Plot->x; y = Plot->y; e = Plot->e;
  86.  
  87. /*** GET DATA ***/
  88.  
  89.    /* FIND list(s) of numbers in file, GET numbers in x,y(,e) arrays */
  90.    while (cb = fgets(cbuf,100,fp)) {
  91.  
  92.       trch('\n',0,cb); trch('\t',' ',cbuf); trch(',',' ',cbuf);
  93.  
  94.       if (!KeyWord(cb,Pict)) {
  95.  
  96.          for (i=0; i<=maxcol; i++) wp[i] = getwrd(&cb);
  97.  
  98.          if (LINE_IS_DATA) {
  99.             if (PlotClosed) {
  100.                /* allocate, link, SET UP FOR NEW PLOT */
  101.                Plot->NextPlot = GetStructPlot(); Plot = Plot->NextPlot;
  102.                x = Plot->x; y = Plot->y; e = Plot->e;
  103.                PlotClosed = FALSE; xcoord = 0;
  104.             }
  105.             else if (Plot->NPts == MAXPOINTS) {
  106.                /* allocate, link, SET UP FOR CONTINUATION OF CURRENT PLOT */
  107.                Continued = TRUE; PrevPlot = Plot;
  108.                Plot->NextPlot = GetStructPlot(); Plot = Plot->NextPlot;
  109.                x = Plot->x; y = Plot->y; e = Plot->e;
  110.             }
  111.             else if (Continued)
  112.                {PrevPlot->Continued = TRUE; Continued = FALSE;}
  113.  
  114.             /*** PAYLOAD ***/
  115.             /* STORE data in arrays, COUNT points */
  116.             *(x++) = ( xcol>=0 ? DATA(xcol) : (FFP)(xcoord++));
  117.             *(y++) = DATA(ycol);
  118.             if (ecol>=0) *(e++) = DATA(ecol);
  119.             Plot->NPts++;
  120.  
  121.             if (debug) {
  122.                 printf("GetDat: x=%f, y=%f", *(x-1), *(y-1));
  123.                 if (ecol>=0) printf(", e=%f", *(e-1));
  124.                 printf("\n");
  125.             }
  126.          }
  127.  
  128.          else {
  129.             /* line does not contain plottable data */
  130.             if (!terse) printf("%s\n",cbuf);
  131.             if (PLOT_OPENED) {
  132.                Pict->NPlt++;
  133.                PlotClosed = TRUE;
  134.             }
  135.          }
  136.       }
  137.    }
  138.    
  139.    if (PLOT_OPENED) Pict->NPlt++;
  140.    return;
  141. }
  142.  
  143.  
  144. /******************/
  145. int KeyWord(cb,Pict)
  146. char *cb;
  147. struct Pict *Pict;
  148. {
  149.    short i;
  150.    
  151.    if (same(cb,"*",1)) {
  152.       if (same(cb,"*SCALFACT*",10)) {
  153.          (void)getwrd(&cb);
  154.          for (i=0; i<=maxcol; i++) {
  155.             if (debug) printf("scalfact >%s<\n", cb);
  156.             fact[i] = atoFFP(getwrd(&cb));
  157.             if (debug) printf("scalfact %f\n", fact[i]);
  158.          }
  159.          return (TRUE);
  160.       }
  161.       else if (same(cb,"*TITLE*",7)) {
  162.          (void)getwrd(&cb); (void)strcpy(title,cb);
  163.          Pict->Title = title;
  164.          return (TRUE);
  165.       }
  166.       else if (same(cb,"*XLABEL*",8)) {
  167.          (void)getwrd(&cb); (void)strcpy(xlabel,cb);
  168.          Pict->XLabel = xlabel;
  169.          return (TRUE);
  170.       }
  171.       else if (same(cb,"*YLABEL*",8)) {
  172.          (void)getwrd(&cb); (void)strcpy(ylabel,cb);
  173.          Pict->YLabel = ylabel;
  174.          return (TRUE);
  175.       }
  176.    }
  177.    return (FALSE);
  178. }
  179.  
  180.  
  181. /*************************************************************************/
  182. struct Remember *Key=NULL;
  183.  
  184. struct Plot *GetStructPlot()
  185. {
  186.    struct Plot *P;
  187.  
  188.    P     = (struct Plot *)AllocRemember(&Key,sizeof(struct Plot),MEMF_CLEAR);
  189.    P->x  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  190.    P->y  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  191.    P->e  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  192.    P->xp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  193.    P->yp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  194.    P->ep = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  195.    P->Reg = (struct PlotRegion *)
  196.                   AllocRemember(&Key,sizeof(struct PlotRegion),MEMF_CLEAR);
  197.    if (debug) {
  198.       printf("GetStructPlot: Plot=%d\n",P);
  199.       printf("...Plot->x=%d, ->y=%d, ->e=%d\n",P->x,P->y,P->e);
  200.       printf("...Plot->xp=%d, ->yp=%d, ->ep=%d\n",P->xp,P->yp,P->ep);
  201.       printf("...Plot->Reg=%d\n",P->Reg);
  202.    }
  203.  
  204.    return(P);
  205. }
  206.  
  207.  
  208. /*******************/
  209. void FreeStructPlot()
  210. {
  211.    FreeRemember(&Key,TRUE);
  212. }
  213.